$(window).resize(function(){
	sideBarPosition();
});
//sideBar定位
function sideBarPosition() {
	var nWinW = $(window).width();

	if( nWinW <= 1200 ) {
		$('#sidebar').css({right:-35});
		$('.sidebar-main-bottom, .shopping-cart').css({left:-35});
	} else {
		$('#sidebar').css({right:0});
		$('.sidebar-main-bottom, .shopping-cart').css({left:0});
	}		
	
}

(function(){
	var timer = null;
	$('#service-list').css({
		top: -($('#service-list').children().length * 30 -30)
	})
	$('.contact-service').hover(function() {
			clearTimeout(timer);
			$('#service-list').stop(false,true);
			$('#service-list').show().animate({left:-100,opacity:1},200);
		} ,function() {
			timer = setTimeout(function(){
					$('#service-list').stop(false,true);
					$('#service-list').animate({left:0,opacity:0},200,function() {
						$(this).hide();
					});
			},500)
	})
})();
		
		
	

//refresh cart
function refreshCart(){
	//do
	return;
	Module.request.ajax({
		url: base + "/refreshCart.jhtml",
		beforeSend:function(){
			$('#sidebar .shopping-cart').unbind('mouseover');
		},
		success: function(data){
			$('#sidebar .cart-num').text(Module.cartManager.getCartNumForCookie());
			//同时更新顶部菜单
			$('.shop_cart').text("购物车("+ Module.cartManager.getCartNumForCookie() +")");
		},
		complete:function(){
			$('#sidebar .shopping-cart').bind('mouseover',refreshCart);
		}
	})
}
		
$(function() {
	
	//top
	 $(window).scroll(function(){  
         if ($(window).scrollTop()>100){  
             $(".go_top").show();  
         }else{  
             $(".go_top").hide();  
         }  
     });  
     //当点击跳转链接后，回到页面顶部位置  
     $(".go_top").click(function(){  
         $('body,html').animate({scrollTop:0},1000);  
         return false;  
	 });  
     
     
     
	sideBarPosition();
	$('.sidebar-main li a').hover(function() {
				var $_tip = $('.mui-mbar-tab-tip', $(this).parent());
				if ( $_tip.length) {
						$_tip.show()
							 .animate({opacity:1, right:35},500)
				}
			}, function() {
				var $_tip = $('.mui-mbar-tab-tip', $(this).parent());
				if ( $_tip.length) {
					$_tip.stop(true, true)
						$_tip.show()
							 .animate({opacity:0, right:70},500)
				}
			})
			
			//初始化购物车的数量
			$('#sidebar .cart-num').text(0);
			//同时更新顶部菜单
			$('.shop_cart').text("购物车(0)")
			
			//移入时刷新购物车数量
			$('#sidebar .shopping-cart').mouseover(refreshCart);
			
			(function() {
				var observer = null;
				//打开bar的面板
				$('.open-panel').click( function(event) {
					//如果是打开历史浏览记录
					if (!panel.isOpen) {
						if( $(this).hasClass('panel-history')) {
							var Observer = function() {
								var temp = '{{each list as v}}'
										   +'<li class="panel-item">'
										   +'<a class="pic" target="_blank" href="{{v.path}}"  title="{{v.name}}" >'
										   +'<img src="{{v.image}}" /></a>'
										   +'<p class="item-name">'
										   +'<a href="{{v.path}}" target="_blank" title="{{v.name}}">{{v.name}}</a>'
										   +'</p>'
										   +'<p class="item-name">'
										   +'<a href="javscript:;" title="{{v.manufacturer}}" target="_blank">{{v.manufacturer}}</a>'
										   +'</p>'
										   +'<p class="item-price">'
										   +'{{if v.isLogin}}'
										   +'<a>￥{{v.price}}</a>'
										   +'{{else}}'
										   +'<a href="javascript:;" class="login_look">登录可见</a>'
										   +'{{/if}}'
										   +'</p>'
										   +'</li>'	
										   +'{{/each}}';
								var _this = this;
								this.update = function(data) {
									this.container = data;
									if( data.isOpen) {
										this.present();
									} else {
										$('.panel-wrap-history ul li').remove();
										$('.panel-wrap-history').hide();
										this.container.waitFinish();
									}
								}
								this.present = function() {
									Module.request.ajax({
										url:base + "/history.jhtml",
										success: function(data) {
											_this.container.finish();
											var d = {list:data};
											var render = template.compile(temp);
											var html = render(d);
											$('.panel-wrap-history ul').append(html);
											$('.history-count').text(data.length);
											$('.panel-wrap-history').show();
											$('.panel-wrap-history .panel-content').show();
											_this.resize();
										},
										error: function(data) {
											_this.container.finish();
											$('.panel-wrap-history').show();
											$('.panel-wrap-history .panel-content').hide();
											$('.panel-wrap-history .empty').show();
										}
									})
								}
								this.resize = function() {
									var nWinHeight = $(window).height(),
										l = $('.panel-wrap-history ul li').length;
									if (l * 126 + 40 > nWinHeight - 50) {
										$('.panel-wrap-history .panel-content').css({
											height: nWinHeight - 50,
											overflowY: 'auto'
										})
									}
									else {
										$('.panel-wrap-history .panel-content').css({
											height: l * 126 + 40 ,
											overflowY: 'hidden'
										})
									}
								},
								//清空浏览记录
								this.clean = function(domain) {
									$('.panel-wrap-history ul li').remove();
									$('.panel-wrap-history .panel-content').hide();
									$('.panel-wrap-history .empty').show();
									$.cookie('_hgicn', ',', {expires: -1, path: '/',domain: domain}); //清空cookie
								}
							}
						}
						observer = new Observer();
						panel.cleanObserver();
						panel.addObserver(observer);
						$(this).addClass('selected');
						panel.open();
					} else {
						$('.bar-panel-close').trigger('click');
					}
					
					var event = event || window.event;
					event.stopPropagation();
					
				})
				
				$('#sidebar').click(function(event) {
					var event = event || window.event;
					var r = event.src || event.target;
					//如果是登陆可以见按钮的话 则让事件向上传播
					if (!$(r).hasClass('login_look')) {
						var event = event || window.event;
						event.stopPropagation();
					}
				})
				 
				$(window).resize(function() {
					if(observer) observer.resize();
				})
				
			})()
})

		  